﻿using DirectoryServicesProtocolsExtensions.MetaData;

namespace DirectoryServicesProtocolsExtensions.SearchRequest
{
    using System.Data;
    using System.DirectoryServices.Protocols;
    using LdapConnection;
    using LdapConnection.MetaData;

    public static class ResultToSql
    {
        public static DataTable BuildDataTableToHoldResults(
            this SearchRequest searchRequest,
            LdapConnection ldapConnection,
            LdapMetaMapFactory ldapMetMapFactory)
        {
            var table = new DataTable();
            foreach (var attribute in searchRequest.Attributes)
            {
               var metaMap = ldapConnection.GetAttributeMetaData(attribute, ldapMetMapFactory);
               var newColumn = new DataColumn(
                   metaMap.LdapDisplayName,
                   metaMap.SemanticDotNetTypeEquivalent ?? metaMap.RawDotNetTypeEquivalent);
               table.Columns.Add(newColumn);    
         }
         
         return table;
        }
    }
}
